package com.starbaba.stat.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.base.common.database.BaseDao;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.stat.model.StatActivityChannelModel;
import com.starbaba.stat.utils.Constant;
import com.tuanzi.server.loan_base.model.db.CommProductPrdid;

@SuppressWarnings("rawtypes")
@Component
public class StatActivityChannelService {

	private static BaseDao testDao = new BaseDao("tt1");
	static BaseDao dao = new BaseDao(Constant.LOAN_STAT_DB_POOL);
	
	public void showPage(Map<String, Object> paramsMap) throws Exception {
		List<CommProductPrdid> prdList = testDao.list("select prdid,name from comm_product_prdid", CommProductPrdid.class);
		List<StatActivityChannelModel> channelList = dao.list("select channel_name from stat_activity_channel group by channel_name", StatActivityChannelModel.class);
		CommProductPrdid bean = new CommProductPrdid();
		bean.setPrdid(0);
		bean.setName("未知");
		prdList.add(0, bean);
		paramsMap.put("prdList", prdList);
		
		StatActivityChannelModel model = new StatActivityChannelModel();
		model.setChannel_name("全部");
		channelList.add(0, model);
		paramsMap.put("channelList", channelList);
	}
	
	public  String getExportString(List<StatActivityChannelModel> list) {
		StringBuilder builder = new StringBuilder();
		builder.append("日期,所属产品,所属平台,渠道标识,渠道名,外部登记用户数,新用户数,激活用户数,激活率\r\n");
		for (StatActivityChannelModel vo : list) {
			builder.append(vo.getDate()).append(","); 
			builder.append(vo.getProd_code()).append(","); 
			builder.append(vo.getPlatform()).append(","); 
			builder.append(vo.getActivity()).append(",");
			builder.append(vo.getChannel_name()).append(","); 
			builder.append(vo.getRegister()).append(","); 
			builder.append(vo.getNew_user()).append(","); 
			builder.append(vo.getActivate()).append(","); 
			builder.append(getPercent(vo.getActivate(), vo.getRegister())).append(","); 
			
			builder.append("\r\n");
		}
		return builder.toString();
	}
	
	public List getList(Integer start, Integer length, PageResponse pageResponse, Map<String, Object> paramsMap) throws Exception {
		
		String startTime = (String) paramsMap.get("startTime");
		String endTime = (String) paramsMap.get("endTime");
		String platform = (String) paramsMap.get("platform");
		String prod_code = (String) paramsMap.get("prod_code");
		String channel = (String) paramsMap.get("channel"); 
		
		StringBuilder whereParams = new StringBuilder();
		if (StringUtils.isNotEmpty(startTime)) {
			whereParams.append("and c.date >= '" + startTime + "' ");
		}
		if (StringUtils.isNotEmpty(endTime)) {
			whereParams.append("and c.date <= '" + endTime + "' ");
		}
		if (StringUtils.isNotEmpty(platform) && !"全部".equals(platform)) {
			whereParams.append("and c.platform = '" + platform + "' ");
		}
		if (StringUtils.isNotEmpty(prod_code) && !"-1".equals(prod_code)) {
			whereParams.append("and c.prod_code = " + prod_code + " ");
		}
		if (StringUtils.isNotEmpty(channel) && !"全部".equals(channel)) {
			whereParams.append("and c.channel_name = '" + channel + "' ");
		}
		
		StringBuilder groupBy = new StringBuilder();
		groupBy.append("group by c.date, ");
		if (!"全部".equals(platform)) {
			groupBy.append("c.platform, ");
		}
		if (!"-1".equals(prod_code)) {
			groupBy.append("c.prod_code, ");
		}
		if (!"全部".equals(channel)) {
			groupBy.append("c.channel_name, ");
		}
		groupBy.append("c.activity ");
		
		StringBuilder listSql = new StringBuilder("select c.date, c.prod_code, c.platform, c.activity, c.channel_name, sum(c.register) register,sum(c.new_user) new_user, sum(c.activate) activate from stat_activity_channel c  where 1=1 ");
		listSql.append(whereParams).append(groupBy);
		listSql.append(" order by c.date desc ");
		
		String sqlCount = "select count(1) from (" + listSql.toString() + ") z ";
		if (start != null && length != null) {
			listSql.append("limit " + start + ", " + length);
		}
		int count = dao.getInt(sqlCount);
		List<StatActivityChannelModel> list = dao.list(listSql.toString(), StatActivityChannelModel.class);
		
		List<CommProductPrdid> prdList = testDao.list("select prdid,name from comm_product_prdid ", CommProductPrdid.class);
		Map<Integer,String> prodMap = new HashMap<Integer, String>();
		prodMap.put(0, "未知");
		prodMap.put(-1, "全部");
		for(CommProductPrdid prod:prdList){
			prodMap.put(prod.getPrdid(), prod.getName());
		}
		
		for (StatActivityChannelModel model : list) {
			if ("全部".equals(platform)) {
				model.setPlatform("全部");
			}
			if ("-1".equals(model.getPlatform())) {
				model.setPlatform("未知");
			}
			
			Integer prodCode = model.getProd_code();
			String prodName = prodMap.get(prodCode);
			model.setPrd_id(prodName);
			
			if ("全部".equals(channel)) {
				model.setChannel_name("全部");
			}
			model.setActivate_rate(getPercent(model.getActivate(), model.getRegister()));
		}
		pageResponse.setITotalRecords(count);
		pageResponse.setITotalDisplayRecords(count);
		return list;
	}
	
	public static String getPercent(Long a, Long b) {
		return (float) Math.round((float) a / b * 10000) / 100 + "%";
	}
	
	public static String getPercent(String a, String b) {
		if (StringUtils.isEmpty(a) || StringUtils.isEmpty(b)){
			return "0";
		}
		Long al = Long.parseLong(a);
		Long bl = Long.parseLong(b);

		return (float) Math.round((float) al / bl * 10000) / 100 + "%";
	}
	
}
